package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.SOrderProductService;
import cn.huiyunche.base.service.mappers.SOrderProductMapper;
import cn.huiyunche.base.service.model.SOrderProduct;
import cn.huiyunche.base.service.model.SOrderProductExample;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SOrderProductServiceImpl implements SOrderProductService {

    private static final Logger logger = LoggerFactory.getLogger(SOrderProductServiceImpl.class);

    @Autowired
    private SOrderProductMapper sOrderProductMapper;

    @Override
    public long add(long customerOrderId, long productId, Date arriveTime) {
        logger.info("add params : {}, {}", customerOrderId, productId);
        if (0 == customerOrderId) {
            logger.error("add customerOrderId must not be 0");
            throw new IllegalArgumentException("客户订单id不能是0！");
        }
        if (0 == productId) {
            logger.error("add productId must not be 0");
            throw new IllegalArgumentException("物流产品id不能是0！");
        }

        SOrderProduct orderProduct = new SOrderProduct();
        orderProduct.setCustomerOrderId(customerOrderId);
        orderProduct.setProductId(productId);
        orderProduct.setArrivalTime(arriveTime);
        sOrderProductMapper.insertSelective(orderProduct);

        return orderProduct.getId();
    }

    @Override
    public Long getProductIdByCustemerOrderId(long customerOrderId) {
        logger.info("getProductIdByCustemerOrderId param : {}", customerOrderId);
        if (0 == customerOrderId) {
            logger.error("getProductIdByCustemerOrderId customerOrderId must not be 0");
            throw new IllegalArgumentException("客户订单id不能是0！");
        }

        SOrderProductExample example = new SOrderProductExample();
        example.createCriteria().andCustomerOrderIdEqualTo(customerOrderId);
        List<SOrderProduct> list = sOrderProductMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0).getProductId();
        }

        return null;
    }

    @Override
    public void deleteByCOrderId(long customerOrderId) {
        logger.info("deleteByCOrderId param : {}", customerOrderId);
        if (0 == customerOrderId) {
            logger.error("deleteByCOrderId customerOrderId must not be 0");
            throw new IllegalArgumentException("客户订单id不能是0");
        }

        SOrderProductExample example = new SOrderProductExample();
        example.createCriteria().andCustomerOrderIdEqualTo(customerOrderId);
        sOrderProductMapper.deleteByExample(example);
    }
}